# -- coding: utf-8 --
# @Time : 2023/3/22 下午8:24
# @Author : guxuan
# @Email : guxuan@qq.com
# @File : LoginMiddle.py
# @Software: PyCharm
# @project: drf21
import base64

from django.http import JsonResponse
from django.utils.deprecation import MiddlewareMixin

from drf21_04.models import student


class LoginMiddleware(MiddlewareMixin):
    def  process_request(self,request):
           try:
               username = base64.standard_b64decode(
                   request.get_signed_cookie('username', salt='abcd').encode('utf-8')).decode('utf-8')
               if request.path !='/drf21_07/logout':
                   try:
                       user = student.objects.get(name=username)
                       request.user = user
                   except:
                      return JsonResponse({
                          'status':404,
                          'msg':'无法查询该用户的信息！'
                      })
               else:
                    pass
           except:
               return JsonResponse({
                   'status': 404,
                   'msg': '您还没有进行登录哦！'
               })